﻿package src.util
{
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import flash.net.registerClassAlias;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.filters.*;
	import flash.filters.BitmapFilterQuality;	
//	import flash.filters.DropShadowFilter;	
	import flash.utils.*;
	import fl.controls.TextInput;
	
	import src.computation.CElement;
	import src.util.Utils;
	
	public class Utils 
	{
		
		public function Utils() {}
			
		public static function getMask(w:Number,h:Number) {
			var rect:Sprite = new Sprite();
			rect.graphics.beginFill(0xffffff);
			rect.graphics.drawRect(0, 0, w, h);
			rect.alpha = 0.5;		
			rect.graphics.endFill();
			return rect;
		}
		
		public static function getGlowFilter(Colour:int):GlowFilter {
			var color:Number = 0xFFFFFF;
			if (Colour < 1) {
				color = Colour;
			}			
            var alpha:Number = 0.8;
            var blurX:Number = 15;
            var blurY:Number = 15;
            var strength:Number = 0.7;
            var inner:Boolean = true;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.HIGH;
            return new GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
		}
		
		public static function getDropShadowFilter(Colour:int):DropShadowFilter {
			var color:Number = 0x000000;
			//if (Colour != null) {
				color = Colour;
			//}			
            var angle:Number = 90;
            var alpha:Number = 0.6;
            var blurX:Number = 12;
            var blurY:Number = 4;
            var distance:Number = 1;
            var strength:Number = 1;
            var inner:Boolean = false;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.LOW;
            return new DropShadowFilter(distance, angle, color, alpha, blurX, blurY, strength, quality, inner, knockout);
		}
		
		public static function sizeDict(dict:Dictionary):Number {
			var i = 0;
			var p;
			if(dict !=null){
				for each(p in dict) {
					i++;
				}
			}
			return i
		}
		
		public static function dictKeys(dict:Dictionary):Array {
			var ar = new Array();
			if(dict!=null){
				for (var key in dict) {
					ar.push(key);
				}
			}
			return ar;
		}
		
		public static function printDict(dict:Dictionary){
			if(dict !=null){
				for (var key in dict){
					trace(key + "=>" + dict[key]);
				}
			}			
		}		
		
		
		/**
		 * clone
		 * not use for displayObject
		 * @param	source
		 * @return	*
		 */
		public static function clone(source:Object):* {			
			var className = Utils.getClass(source);		
			registerClassAlias(getQualifiedClassName(source), className);
			var copier:ByteArray = new ByteArray();
			copier.writeObject(source);
			copier.position = 0;		
			var newobj = copier.readObject();			
			var cloneObject = newobj as className;
			return cloneObject;
		}
		
		public static function getClass(obj:Object):Class {
			return Class(getDefinitionByName(getQualifiedClassName(obj)));
		}
		
		public static function numberInputText() : TextInput {
			var textInput : TextInput = new TextInput();
			textInput.width = 150;
			textInput.restrict = "0-9|.";
			textInput.maxChars = 20;
			return textInput;
		}	
			
	}	
		
}